﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DB = HTComics.Data;
using System.Data;
using System.Net.Mail;
using HTComics.Web.Common;

namespace HTComics.Web.User
{
    public partial class Signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Visible = false;
        }

        protected void btnSignup_Click(object sender, EventArgs e)
        {
            DataTable dt = DB.HTCUser.Signup(txtFirstname.Text, txtLastname.Text, txtLogin.Text, txtPassword.Text, txtEmail.Text);

            if (dt.Rows[0][0].ToString() == "-1") //Username already exists
            {
                lblError.Text = "Sorry, this username already exist in the system, please choose another one";
                lblError.Visible = true;
            }
            else if (dt.Rows[0][0].ToString() == "-2") //Email already exists
            {
                lblError.Text = "Sorry, this email is already associated with another account, please provide a different email";
                lblError.Visible = true;
            }
            else
            {
                try
                {
                    MailMessage msg = new MailMessage();
                    msg.To.Add(txtEmail.Text);
                    msg.Subject = "Hidden Treasure Comics Account Password";
                    msg.Body = "Congratulations, you have successfully signed up an account with Hidden Treasure Comics.<br /><br /><b>Username:</b> " + txtLogin.Text + "<br/><b>Password: </b>" + txtPassword.Text;
                    msg.From = new MailAddress(Defaults.MailSetup.NoReplyAccount);


                    SmtpClient mailClient = new SmtpClient(Defaults.MailSetup.MailServer, Defaults.MailSetup.MailPort);
                    mailClient.Send(msg);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    lblSuccess.Visible = true;
                    mainDiv.Visible = false;

                    //Initiate User Session
                    Session["UID"] = dt.Rows[0][0].ToString();
                    Session["FirstName"] = txtFirstname.Text;
                    Session["LastName"] = txtLastname.Text;
                    Session["Email"] = txtEmail.Text;

                    if (Request.QueryString["ord"] != null && Request.QueryString["ord"] == "1") //For Submit Ord
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=text/javascript>window.setTimeout(function(){window.location='" + ResolveUrl("~/Order/SubmitOrder.aspx") + "'},10000)</script>");
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type=text/javascript>window.setTimeout(function(){window.location='" + ResolveUrl("~/Default.aspx") + "'},10000)</script>");
                }
            }
        }
    }
}